package org.nicocube.airain.app.client.ui;

import java.util.Date;
import java.util.Map;

import org.nicocube.airain.app.client.AirainApp;
import org.nicocube.airain.app.client.PlayerRegistrationServiceAsync;
import org.nicocube.airain.common.client.form.FormVisitor;
import org.nicocube.airain.common.client.form.NamedWidgetProvider;
import org.nicocube.airain.common.client.table.Item;
import org.nicocube.airain.common.client.table.TableOfItem;
import org.nicocube.airain.domain.client.character.Gender;
import org.nicocube.airain.domain.client.util.Publisher;
import org.nicocube.airain.domain.client.util.Subscriber;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PlayerSubscriptionPanel extends Composite {

	public static interface I18nConstants extends Constants,AirainApp.I18nConstants {

		@DefaultStringMapValue({
			"firstName","First name:",
			"lastName","Last name:",
			"email","Email:",
			"dateOfBirth","Date of birth:",
			"gender","Gender:",
			"country","Country:",
			"language","Language:",
			"acceptNewsLetter","Accept newsletter:",
			"acceptPromotion","Accept promotion email:",
			"acceptPartner","Accept partner email:"
		})
		Map<String, String> fieldsLabel();

		@DefaultStringValue("Are you sure to save now ?")
		String sureToSave();

		@DefaultStringValue("Your information was successfully saved. You will be redirected now.")
		String successSave();

		@DefaultStringValue("Send")
		String sendSubscription();
	}

	private I18nConstants constants;

	private final String email;

	private final PlayerRegistrationServiceAsync playerRegistrationServiceAsync;

	private final VerticalPanel vp;

	public PlayerSubscriptionPanel(String email,PlayerRegistrationServiceAsync playerRegistrationServiceAsync) {
		this.email = email;
		this.playerRegistrationServiceAsync = playerRegistrationServiceAsync;
		constants = GWT.create(I18nConstants.class);
		vp = new VerticalPanel();
		initWidget(vp);
	}

	@Override
	protected void onLoad() {
		super.onLoad();

		final PlayerForm playerForm = new PlayerForm();
		playerForm.setEmail(email);
		playerForm.setGender(Gender.Unknown);
		playerForm.setDateOfBirth(new Date());

		final TableOfItem formData = new TableOfItem();

		playerForm.visitFormFields(constants,new FormVisitor() {
			@Override
			public void visit(NamedWidgetProvider f) {
				formData.addRow(new Item().setIdent(constants.fieldsLabel().get(f.getName())).addWidget(f.getWidget()));
			}
		});

		vp.add(formData.render());

		Button button = new Button(constants.sendSubscription());
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (Window.confirm(constants.sureToSave())) {
					playerRegistrationServiceAsync.save(playerForm, new AsyncCallback<Void>() {
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("error:"+caught.getMessage());
						}
						@Override
						public void onSuccess(Void result) {
							Window.alert(constants.successSave());
							Location.reload();
						}
					});
				}
			}
		});

		vp.add(button);

		final Label l = new Label();

		playerForm.subscribe(new Subscriber() {
			@Override
			public void update(Publisher publisher) {
				l.setText(publisher.toString());
			}
		});

		vp.add(l);
	}
}
